// packageA/pages/equipmentCheckList/equipmentCheckList.js
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //列表
    list:[],
    //无数据
    showNothing:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);

      console.log('检测传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      //获取列表数据
      this.getList();
    } 
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'testEquipmentRecord',
      data: {
        equipmentNo: this.data.paramObj.equipmentNo,
        selectFields: ['id','startDate','endDate','testMan','isWork','isClean','facade','runTime','remark','date'],
        sort: {
          id: -1
        }
      }
    }  

    CommonService.goFindList(param).then(res => {
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('检测列表');
        console.log(result);

        this.setData({
          list: result
        })
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })
    this.getList();
  },

  //跳转添加
  goToAdd(){
    let obj={
      repertoryId: this.data.paramObj.repertoryId
    }

    wx.navigateTo({
      url: "/packageA/pages/equipmentCheckAdd/equipmentCheckAdd?paramObj="+JSON.stringify(obj)
    })
  },

  //添加完成
  onComplete(){
    Dialog.confirm({
      title: '不再添加检测数据了吗',
      message:'确定后，设备状态更新为已完成',
      confirmButtonText:'确定',
      cancelButtonText:'还要再加'
    }).then(() => {
      let param={
        className:'repertory',
        data:{
          id: this.data.paramObj.repertoryId,
          state: 4
        }
      }
  
      CommonService.goSave(param).then(res=>{
        if (res.statusCode == 200 && res.data.success == true){
          Toast.success('成功');
          setTimeout(() => {
            //返回列表页并刷新数据
            this.backAndRefresh();
          }, 1000);
        }else{
          Toast.fail('操作失败');
        }
      }).catch((error) => {
        Toast.fail('操作失败');
      })  

    }).catch(() => {
    });
  },

  //返回列表页并刷新数据
  backAndRefresh(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];

    prevPage.refreshListAndPage();
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})